package com.malou.client.util;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.event.dom.client.KeyCodes;

public class Utilities {
	public static Integer itemsPerPage = 20;
	public static Double igv_value = 1.18;
	public static String tienda = null;
	
	public static boolean hasValue(TextField<String> field) {
	    return field.getValue() != null && field.getValue().length() > 0;
	}
	public static boolean hasValue(NumberField field) {
	    return field.getValue() != null && field.getValue().toString().length() > 0;
	}
	public static String toIds(List<BeanModel> list) {
		String ids="";
		for(BeanModel item : list){						
			ids += ","+item.get("id");
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static String toIdString(List<Integer> list) {
		String ids="";
		for(Integer id : list){						
			ids += ","+id;
		 }
		 ids = ids.substring(1);
		return ids;
	}
	public static List<Integer> toListIds(List<BeanModel> list) {
		List<Integer> retorno = new ArrayList<Integer>();		
		for(BeanModel item : list){			
			retorno.add(Integer.valueOf(item.get("id").toString()));
		 }
		return retorno;
	}
	
	public static void focoEnter(final Component comp, final Component comp2) {

		comp.addListener(Events.KeyDown, new KeyListener() {

			public void componentKeyDown(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c == KeyCodes.KEY_ENTER)
					comp2.focus();

			}

		});

	}

	public static void Enter(Component A[]) {
		for (int i = 0; i < A.length - 1; i++)
			focoEnter(A[i], A[i + 1]);

	}
	
	public static void setDigitos(final Component txtField,final Integer lengh) {

		txtField.addListener(Events.KeyPress, new KeyListener() {

			@SuppressWarnings("unchecked")
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c > 31 && (c < 48 || c > 57)|| String.valueOf(((TextField<Number>) txtField).getValue()).length() == lengh)
					event.preventDefault();
			}

		});

	}
	
	public static void setNumeric(final TextField<String> txtField) {

		txtField.addKeyListener(new KeyListener() {
			public void componentKeyPress(ComponentEvent event) {
				Integer c = event.getKeyCode();

				if (c > 31 && (c < 48 || c > 57) )
					event.preventDefault();

			}

		});

	}
	
}
